package com.smartwebx.ha.web.util.kml;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.smartwebx.ha.util.KmlArea;
import com.smartwebx.ha.util.KmlPoint;
import com.smartwebx.ha.util.KmlProperty;

public class GetAreaKml {
	/*public KmlProperty readKml() throws IOException{
		KmlProperty kmlProperty = new KmlProperty();
		List<KmlArea> rgbListInit= new ArrayList<>();
		KmlArea kmlArea1 = new KmlArea("00FFFF", "0", "122.79013458925772,33.298459903020252,122.66715857076402,33.298459903020252,122.66715857076402,33.371454577078772,122.79013458925772,33.371454577078772,122.79013458925772,33.298459903020252", "1");
		KmlArea kmlArea2 = new KmlArea("00B2EE", "0", "122.79013458925772,33.371454577078772,122.66715857076402,33.371454577078772,122.66715857076402,33.444449251137293,122.79013458925772,33.444449251137293,122.79013458925772,33.371454577078772", "1");
		KmlArea kmlArea3 = new KmlArea("0000FF", "0", "122.79013458925772,33.444449251137293,122.66715857076402,33.444449251137293,122.66715857076402,33.517443925195813,122.79013458925772,33.517443925195813,122.79013458925772,33.444449251137293", "1");
		KmlArea kmlArea4 = new KmlArea("EEDC82", "0", "122.79013458925772,33.517443925195813,122.66715857076402,33.517443925195813,122.66715857076402,33.590438599254334,122.79013458925772,33.590438599254334,122.79013458925772,33.517443925195813", "1");
		KmlArea kmlArea5 = new KmlArea("EE9A00", "0", "122.79013458925772,33.590438599254334,122.66715857076402,33.590438599254334,122.66715857076402,33.663433273312847,122.79013458925772,33.663433273312847,122.79013458925772,33.590438599254334", "1");
		KmlArea kmlArea6 = new KmlArea("EE30A7", "0", "122.79013458925772,33.663433273312847,122.66715857076402,33.663433273312847,122.66715857076402,33.736427947371368,122.79013458925772,33.736427947371368,122.79013458925772,33.663433273312847", "1");
		KmlArea kmlArea7 = new KmlArea("EE4000", "0", "122.79013458925772,33.736427947371368,122.66715857076402,33.736427947371368,122.66715857076402,33.809422621429889,122.79013458925772,33.809422621429889,122.79013458925772,33.736427947371368", "1");
		KmlArea kmlArea8 = new KmlArea("EE3B3B", "0", "122.79013458925772,33.809422621429889,122.66715857076402,33.809422621429889,122.66715857076402,33.882417295488409,122.79013458925772,33.882417295488409,122.79013458925772,33.809422621429889", "1");
		KmlArea kmlArea9 = new KmlArea("EE2C2C", "0", "122.79013458925772,33.882417295488409,122.66715857076402,33.882417295488409,122.66715857076402,33.95541196954693,122.79013458925772,33.95541196954693,122.79013458925772,33.882417295488409", "1");
		KmlArea kmlArea10 = new KmlArea("EE0000", "0", "122.79013458925772,33.95541196954693,122.66715857076402,33.95541196954693,122.66715857076402,34.028406643605443,122.79013458925772,34.028406643605443,122.79013458925772,33.95541196954693", "1");
		
		rgbListInit.add(kmlArea1);
		rgbListInit.add(kmlArea2);
		rgbListInit.add(kmlArea3);
		rgbListInit.add(kmlArea4);
		rgbListInit.add(kmlArea5);
		rgbListInit.add(kmlArea6);
		rgbListInit.add(kmlArea7);
		rgbListInit.add(kmlArea8);
		rgbListInit.add(kmlArea9);
		rgbListInit.add(kmlArea10);
		
		
		File file = new File("D:"+File.separator+"等值面.txt");
		char[] c= new char[(int)file.length()];
		Reader input = new FileReader(file);
		int len = input.read(c);
		String str= new String(c, 0, len).replaceAll(":", ",");
		String[] s1=str.split("\\|");
		List<KmlArea> kmlAreaList= new ArrayList<KmlArea>();
		MyComparator comparator = new MyComparator();
		Set<String> valSet = new TreeSet<>(comparator);
		List<KmlPoint> kmlPoints = new ArrayList<>();
		for(String str1:s1){
			KmlPoint kmlPoint = new KmlPoint();
			String[] s2= str1.split(",a,");
			String[] points=s2[0].split(",");
			
			String[] valAndProperty= s2[1].split(",");
			KmlArea kmlArea = new KmlArea("",valAndProperty[0],s2[0],valAndProperty[1]);
			//数值标点
			kmlPoint.setPoints(points[points.length-2]+","+points[points.length-1]);
			kmlPoint.setPointVal(valAndProperty[0]);
			kmlPoints.add(kmlPoint);
			
			kmlAreaList.add(kmlArea);
			valSet.add(valAndProperty[0]);
		}
		List<KmlArea> kmlColorCode = new ArrayList<>();
		int i=0;
		for(Iterator<String> iterator  = valSet.iterator();iterator.hasNext();){
			String val=iterator.next();
			rgbListInit.get(i).setLineVal(val);
			kmlColorCode.add(rgbListInit.get(i));
			KmlPoint kmlPoint = new KmlPoint();
			String[] colorCodes= rgbListInit.get(i).getPoints().split(",");
			kmlPoint.setPoints(colorCodes[colorCodes.length-2]+","+colorCodes[colorCodes.length-1]);
			kmlPoint.setPointVal(val);
			kmlPoints.add(kmlPoint);
			i++;
		}
		System.out.println(kmlAreaList);
		for(int j=0;j<kmlAreaList.size();j++){
			for(int m=0;m<kmlColorCode.size();m++){
				String areaLineVal,areaLineProperty,codeLineVal;
				areaLineVal=kmlAreaList.get(j).getLineVal();
				areaLineProperty=kmlColorCode.get(m).getLineVal();
				codeLineVal=kmlAreaList.get(j).getProperty();
				if(areaLineVal.equals(areaLineProperty) && codeLineVal.equals("1")){
					kmlAreaList.get(j).setAreaColor(kmlColorCode.get(m).getAreaColor());
				}else if(areaLineVal.equals(areaLineProperty) && codeLineVal.equals("-1")){
					if(m==0){
						kmlAreaList.get(j).setAreaColor(kmlColorCode.get(m).getAreaColor());
					}else{
						kmlAreaList.get(j).setAreaColor(kmlColorCode.get(m-1).getAreaColor());
					}
					
				}
			}
		}
		kmlColorCode.addAll(kmlAreaList);
		kmlProperty.setKmlAreas(kmlColorCode);
		kmlProperty.setKmlPoints(kmlPoints);
		return kmlProperty;
		
	}
	
	public static class MyComparator implements Comparator<String>{

		@Override
		public int compare(String o1, String o2) {
			if(Integer.parseInt(o1)>Integer.parseInt(o2)){
				return 1;
			}else if(Integer.parseInt(o1)==Integer.parseInt(o2)){
				return 0;
			}else{
				return -1;
			}
		}
	}*/

}
